package no.logic;

import android.content.Context;
import android.text.GetChars;
import android.util.Log;
import no.testspill.Ball2;
import no.testspill.LineSegment;

public class Collision {
	
	public static double[] computeNewDirection(float ballStartX, float ballStartY,  float[] ballVek, float barrierVek[], float endX, float endY){
		//Find Angle
		double ballVekLength =Math.sqrt(Math.pow(ballVek[0], 2) + Math.pow(ballVek[1], 2));
//		System.out.println("ballVekLength: " + ballVekLength);
		double barrierVekLength = Math.sqrt(Math.pow(barrierVek[0], 2) + Math.pow(barrierVek[1], 2));
//		System.out.println("BarrierVekLength: " + barrierVekLength);
		double cosAngle = ((ballVek[0]*barrierVek[0])+(ballVek[1]*barrierVek[1]))/(ballVekLength*barrierVekLength); 
//		System.out.println("angle: " + cosAngle); 
		
		//Find unit vector
		double unitDx = barrierVek[0]/barrierVekLength;
//		System.out.println("untiDX "+unitDx);
		double unitDy = barrierVek[1]/barrierVekLength;
//		System.out.println("untiDY "+unitDy);
		double[] unitBarrierVek = {unitDx,unitDy};
		
		//Find new vector
		double length = 2 * ballVekLength * cosAngle;
//		System.out.println("length "+ length);
		double newVector[] = {unitBarrierVek[0]*length , unitBarrierVek[1]*length};
//		System.out.println("newVector" + newVector[0] + "," + newVector[1]);
		
		//Find new points ***have to fix this one***
		double newDx = ballStartX+newVector[0];
//		System.out.println("newDx "+newDx);
		double newDy = ballStartY+newVector[1];
//		System.out.println("newDy "+newDy);
		double newCoords[] = {newDx - endX, newDy - endY};
		return newCoords;
	}
	
	public static void reflect(Ball2 ball, LineSegment line){
		float[] lineVek = {line.getEndX() - line.getStartX() , line.getEndY()-line.getStartY()};
		float[] ballVek = {ball.getDx(), ball.getDy()};
		double newCoords[] = computeNewDirection(line.getCollisionPointX()-ball.getDx(), line.getCollisionPointY()-ball.getDy(), ballVek, lineVek, line.getCollisionPointX(), line.getCollisionPointY());
		ball.setDx((float) newCoords[0]);
//		System.out.println("dX: " + ball.getDx());
		ball.setDy((float) newCoords[1]);
		ball.normalize();
	}
	
	
	

}
